package 斐波那契数;

/**
 * createTime: 2025/4/28 23:28
 * description:
 *
 * @author AirMan
 */
public class Solution2 {
    public int fib(int n) {
        // 优化dp，观察dp发现，只需要记录前一个，和前一个的前一个数值即可

        if (n == 0 || n == 1)
            return n;

        int[] dp = new int[2];
        dp[0] = 0;
        dp[1] = 1;
//        System.out.println(dp[0] + "," + dp[1]);
        for (int i = 2; i <= n; i++) {
            int curNum = dp[0] + dp[1];
//            System.out.println("curNum = " + curNum);
            dp[0] = dp[1];
            dp[1] = curNum;
//            System.out.println(dp[0] + "," + dp[1]);
        }

        return dp[1];
    }
}
